package yjl.spec.edu.week11_class023;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import yjl.spec.edu.week11_class023.R;

class FirstAdapter extends ArrayAdapter<First>{
    private int resourceId;

    public FirstAdapter(Context context, int textViewResourceId, List<First> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        First list = getItem(position); // 获取当前项的List实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.listImage = (ImageView) view.findViewById (R.id.list_image);
            viewHolder.listMesg = (TextView) view.findViewById (R.id.list_mesg);
            viewHolder.listTitle = (TextView) view.findViewById (R.id.list_title);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        viewHolder.listImage.setImageResource(list.getImageId());
        viewHolder.listMesg.setText(list.getMesg());
        viewHolder.listTitle.setText(list.getTitle());
        return view;
    }

    class ViewHolder {

        ImageView listImage;
        TextView listMesg;
        TextView listTitle;

    }
}
